﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageUploader.domain;
using System.IO;

namespace ImageUploader.GUI
{
    public partial class EditForm : Form
    {
        public User currentUser;
        private bool enableSave;

        public EditForm(User currentUser)
        {
            InitializeComponent();
            this.currentUser = currentUser;
            FillControls();
        }

        private void FillControls()
        {
            textBoxSAMAccountName.Text = currentUser.SAMAccountName;
            textBoxCn.Text = currentUser.CN;
            textBoxUserPrincipalName.Text = currentUser.UserPrincipalName;
            textBoxFirstName.Text = currentUser.FirstName;
            textBoxLastName.Text = currentUser.LastName;
            textBoxMail.Text = currentUser.Mail;
            textBoxDisplayedName.Text = currentUser.DisplayName;
            textBoxPhone.Text = currentUser.Telnr;
            textBoxPostalCode.Text = currentUser.PostalCode;
            textBoxPostalOfficeBox.Text = currentUser.PostOfficeBox;
            textBoxOffice.Text = currentUser.Office;
            textBoxWebpage.Text = currentUser.HomePage;
            textBoxInitials.Text = currentUser.Initials;
            textBoxDescription.Text = currentUser.Description;
            textBoxProvince.Text = currentUser.Province;
            textBoxStreet.Text = currentUser.Street;
            textBoxPlace.Text = currentUser.Place;
            textBoxCountry.Text = currentUser.Country;
            if (currentUser.JpgPhoto != null)
                pictureBoxOld.Image = ImageOperations.GetImageFromByteArray(currentUser.JpgPhoto);
        }

        public String ValidateSave()
        {
            String errortext = "";

            if (textBoxCn.Text == "" || textBoxSAMAccountName.Text == "")
                errortext = "CN and sAMAccountName are required";
            else
                if (textBoxCn.Text == "New account" || textBoxSAMAccountName.Text == "New account")
                    errortext = "Rename CN or sAMAccountName";
                else
                    errortext = "";

            return errortext;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (ValidateSave() != "")
            {
                labelMessage.Text = ValidateSave();
                return;
            }
            else
                labelMessage.Text = "";

            if (pictureBoxNew.Image != null)
            {
                currentUser.JpgPhoto = ImageOperations.GetByteArrayFromJpgImage(pictureBoxNew.Image);
                textBoxImageURL.Text = "";
                pictureBoxOld.Image = ImageOperations.GetImageFromByteArray(currentUser.JpgPhoto);
                pictureBoxNew.Image = null;
            }
            if (pictureBoxOld.Image == null)
                currentUser.JpgPhoto = null;
            
            currentUser.SAMAccountName = textBoxSAMAccountName.Text;
            currentUser.LastName = textBoxLastName.Text;
            currentUser.Office = textBoxOffice.Text;
            currentUser.Description = textBoxDescription.Text;
            currentUser.FirstName = textBoxFirstName.Text;
            //currentUser.AccountEnabled = 1;
            currentUser.UserPrincipalName = textBoxUserPrincipalName.Text;
            currentUser.Mail = textBoxMail.Text;
            currentUser.DisplayName = textBoxDisplayedName.Text; ;
            currentUser.Telnr = textBoxPhone.Text;
            currentUser.PostalCode = textBoxPostalCode.Text;
            currentUser.PostOfficeBox = textBoxPostalOfficeBox.Text;
            currentUser.HomePage = textBoxWebpage.Text; ;
            currentUser.Initials = textBoxInitials.Text; ;
            currentUser.Province = textBoxProvince.Text;
            currentUser.Street = textBoxStreet.Text;
            currentUser.Place = textBoxPlace.Text;
            currentUser.Country = textBoxCountry.Text;
            currentUser.RenameCurrentUser(textBoxCn.Text);
            labelMessage.Text = "succesfully saved " + currentUser.CN;
            this.Close();
            this.DialogResult = DialogResult.OK;
        }

        private void buttonBrowseImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "JPG afbeeldingen (*.jpeg, *.jpg)|*jpeg;*.jpg";
            if (ofd.ShowDialog() == DialogResult.OK)
                textBoxImageURL.Text = ofd.FileName;
        }

        private void textBoxImageURL_TextChanged(object sender, EventArgs e)
        {
            if (File.Exists(textBoxImageURL.Text))
            {
                try
                {
                    Image img = ImageOperations.GetJpgImageFromPath(textBoxImageURL.Text);
                    pictureBoxNew.Image = Image.FromFile(textBoxImageURL.Text);
                    labelMessage.Text = "";
                    this.Refresh();
                    enableSave = true;
                    UpdateSavebuttonEnabledState();
                    return;
                }
                catch (Exception ex)
                {
                    labelMessage.Text = ex.Message;
                    enableSave = false;
                }
            }
            else
            {
                labelMessage.Text = "Image file does not exist";
                enableSave = false;
            }

            if (textBoxImageURL.Text == "")
            {
                labelMessage.Text = "";
                enableSave = true;
            }

            pictureBoxNew.Image = null;
            UpdateSavebuttonEnabledState();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();

        }

        private void UpdateSavebuttonEnabledState()
        {
            buttonSave.Enabled = enableSave;
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxImageURL.Text = "";
            currentUser.JpgPhoto = null;
            pictureBoxNew.Image = null;
            pictureBoxOld.Image = null;
            this.Update();
        }
    }
}
